package wsc.mcis.androidDevelpment;

import java.util.ArrayList;
import java.util.Vector;
import java.util.ListIterator;
import java.net.URLEncoder;

public class Row {

  ArrayList<Data> data = new ArrayList<Data>();
  private String rowId;

  private String submitTo;
  public Vector<Column> columns;

  public Row() {

    this.columns = new Vector<Column>();
    this.rowId = "";
    submitTo = "loopback"; // do nothing but display the results
  }

  // getters & setters
  public String getFormNumber() {
    return rowId;
  }

  public void setFormNumber(String rowId) {
    this.rowId = rowId;
  }

  public String getSubmitTo() {
    return submitTo;
  }

  public void setSubmitTo(String submitTo) {
    this.submitTo = submitTo;
  }

  public Vector<Column> getFields() {
    return columns;
  }

  public void setFields(Vector<Column> columns) {
    this.columns = columns;
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("Row:\n");
    sb.append("Row ID: " + this.rowId + "\n");
    sb.append("Submit To: " + this.submitTo + "\n");
    if (this.columns == null)
      return sb.toString();
    ListIterator<Column> li = this.columns.listIterator();
    while (li.hasNext()) {
      sb.append(li.next().toString());
    }

    return sb.toString();
  }

  public String getFormattedResults() {
    StringBuilder sb = new StringBuilder();
    sb.append("Results:\n");
//    if (this.columns == null)
//      return sb.toString();
//    ListIterator<Column> li = this.columns.listIterator();
//    while (li.hasNext()) {
//      sb.append(li.next().getFormattedResult() + "\n");
//    }

    return sb.toString();
  }

  public String getFormEncodedData() {
    try {
      int i = 0;
      StringBuilder sb = new StringBuilder();
      sb.append("Results:\n");
      if (this.columns == null)
        return sb.toString();
      ListIterator<Column> li = this.columns.listIterator();
      while (li.hasNext()) {
        if (i != 0)
          sb.append("&");
        Column thisColumn = li.next();
        sb.append(thisColumn.name + "=");
        String encstring = new String();
//        URLEncoder.encode((String) thisColumn.getData(), encstring);
        sb.append(encstring);
      }

      return sb.toString();
    } catch (Exception e) {
      return "ErrorEncoding";
    }
  }

}
